package com.library.Service;

import com.library.Entity.Message;
import com.library.Entity.User;
import com.library.Mapper.UserMapper;
import com.library.Util.GetSqlSession;
import com.library.Util.StringUtil;
import org.apache.ibatis.session.SqlSession;

/*
   用于获取用户名和密码是否有效
 */
public class UserService {

    public static Message userlogin(String username, String password) {
      Message message = new Message();
      User user = new User();

      //将用户输入信息存储起来
      user.setUsername(username);
      user.setPassword(password);
      message.setObject(user);

      if(StringUtil.isEmpty(username) || StringUtil.isEmpty(password)){
          //判定获取的用户名和密码是否为空
          message.setCode(0);
          message.setMessage("用户名或密码不能为空");
          return message;
      }

        SqlSession session = GetSqlSession.createSqlSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);
        User u =  userMapper.queryUserByName(username);

        if(u == null){
            message.setCode(0);
            message.setMessage("用户不存在");
            return message;
        }

        if(!password.equals(u.getPassword())){
            message.setCode(0);
            message.setMessage("密码不正确");
            return message;
        }
       message.setObject(u);

      return message;
    }
}
